#  -*- coding: utf-8 -*-

from trading.backtest import Backtest
from strategy.strategy_option import StrategyOption
from util.database import base_code_path
import sys, traceback, os



class Strategy:
    def __init__(self, name):
        # 策略的属性定义
        properties = dict()
        strategy_path = f"{base_code_path}/strategy/strategies"
        self.name = name
        strategy_file = os.path.join(strategy_path, self.name)
        if os.path.exists(strategy_file) is False:
            print(strategy_file, flush=True)
            print("策略名文件名称有误：%s，请确认后重新输入。" % self.name, flush=True)
            return

        with open(strategy_file, encoding='UTF-8') as contents:
            for line in contents:
                if line.startswith("#") is False:
                    if line.index('=') > 0:
                        line = line.replace('\n', '')
                        configs = line.split('=')
                        properties[configs[0]] = configs[1]

        self.strategy_option = StrategyOption(properties)
        #print(self.strategy_option)

    def backtest(self):
        self.strategy_option.stock_pool().get_option_stocks()
        backtest = Backtest(self.strategy_option)
        backtest.start()
        self.strategy_option.stock_pool().export_feature_data()

if __name__ == '__main__':
    # if len(sys.argv) == 3:
    #     if sys.argv[1] == '--name':
    #         strategy = Strategy(sys.argv[2])
    #         strategy.strategy_option.stock_pool().get_option_stocks()
    #         #strategy.backtest()
    # else:
    #     print("启动回测的方式：python strategy_module.py --name strategy_name")
    #     print("例如：python strategy_module.py --name low_pe_strategy")

    # strategy = Strategy("short_strength_strategy")
    # strategy.backtest()
    # strategy.strategy_option.stock_pool().get_option_stocks()

    strategy = Strategy("trending_strategy")
    strategy.strategy_option.stock_pool().get_option_stocks()

    strategy = Strategy("trending_speedup_strategy")
    #strategy.backtest()
    strategy.strategy_option.stock_pool().get_option_stocks()
    #strategy.strategy_option.stock_pool().export_feature_data()
